package com.lablzh.gdxy.service.impl;

import com.lablzh.gdxy.component.DynamicOperateExcelUtils;
import com.lablzh.gdxy.entity.PushfreeProject;
import com.lablzh.gdxy.entity.Sciachievement;
import com.lablzh.gdxy.entity.design.Design;
import com.lablzh.gdxy.repository.PushfreeProjectRepository;
import com.lablzh.gdxy.service.FileService;
import com.lablzh.gdxy.service.PushfreeProjectService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@Service
public class PushfreeProjectServiceImpl implements PushfreeProjectService {
    private static final String BASEPATH = "./resources/static/";
    @Autowired
    private FileService fileService;
    @Autowired
    PushfreeProjectRepository pushfreeProjectRepository;
    @Override
    public  String addpushfreeproject(PushfreeProject pushfreeProject)
    {
        pushfreeProjectRepository.save(pushfreeProject);
        return "添加成功";
    }
    @Override
    public  String deletepushfreeproject(int id){
        pushfreeProjectRepository.deleteById(id);
        return "删除成功";
    }
    @Override
    public  String auditpushfreeproject(int id, String audit)
    {
        PushfreeProject pushfreeProject=new PushfreeProject();
        pushfreeProject=pushfreeProjectRepository.findById(id);
        pushfreeProject.setAudit(audit);
        pushfreeProjectRepository.save(pushfreeProject);
        return "审核成功";
    }
    @Override
    public    PushfreeProject selectpushfreeproject(int id)
    {

        // pushfreeProject pushfreeProject=new pushfreeProject();

        return pushfreeProjectRepository.findById(id);
    }
    @Override
    public List <PushfreeProject> selectstudentpushfreeproject(String studentCode)
    {
        //   List<pushfreeProject> pushfreeProjectList=null;
        return pushfreeProjectRepository.findAllByStuCode(studentCode,Sort.by(Sort.Direction.DESC,"id"));
    }
    @Override
    public    List <PushfreeProject>  selectallpushfreeproject()
    {
        //  List<pushfreeProject> pushfreeProjectList=null;
        return pushfreeProjectRepository.findAll(Sort.by(Sort.Direction.DESC,"id"));
    }

    @Override
    public List<PushfreeProject> selectPushfreeProjectbyaudit(String audit) {
        return pushfreeProjectRepository.findAllByAudit(audit,Sort.by(Sort.Direction.DESC,"id"));
    }

    @Override
    public void downloadExcel(PushfreeProject pushfreeProject1, HttpServletRequest request, String sort, HttpServletResponse response) throws FileNotFoundException {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("stuCode", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher(" name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("major", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("classes", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("grade", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.contains());

        Example<PushfreeProject> example = Example.of(pushfreeProject1, exampleMatcher);
        List<PushfreeProject> pushfreeProjectList = pushfreeProjectRepository.findAll(example);

        if (sort.equals("grade")) {
            pushfreeProjectList.sort(Comparator.comparing(PushfreeProject::getGrade));
        }  else if (sort.equals("studentUsername")) {
            pushfreeProjectList.sort(Comparator.comparing(PushfreeProject::getStuCode));
        } else if (sort.equals("major")) {
            pushfreeProjectList.sort(Comparator.comparing(PushfreeProject::getMajor));
        } else if (sort.equals("classes")) {
            pushfreeProjectList.sort(Comparator.comparing(PushfreeProject::getClasses));
        }
        DynamicOperateExcelUtils dynamicOperateExcelUtils = new DynamicOperateExcelUtils(BASEPATH + "保研加分学生汇总表模板.xlsx");
        List<Map<String, Object>> datas = new ArrayList<>();
        for (PushfreeProject pushfreeProject : pushfreeProjectList) {
            Map<String, Object> data = new HashMap<>();
            data.put("学号", pushfreeProject.getStuCode());
            data.put("姓名", pushfreeProject.getName());
            data.put("年级", pushfreeProject.getGrade());
            data.put("专业", pushfreeProject.getMajor());
            data.put("班级", pushfreeProject.getClasses());
            data.put("获奖项目名称", pushfreeProject.getProjectName());
            data.put("成果类型",pushfreeProject.getType());
            data.put("学生排名",pushfreeProject.getRanknumber());
            data.put("其他信息",pushfreeProject.getDetail());
            datas.add(data);
        }

        if (!datas.isEmpty()) {
            dynamicOperateExcelUtils.appendRows(5, datas,
                    new String[]{"学号", "姓名", "年级", "专业", "班级", "获奖项目名称", "成果类型",
                            "学生排名",  "其他信息"});
        } else {
            dynamicOperateExcelUtils.replaceCellValue(5, 0, "无相关记录");
        }
        dynamicOperateExcelUtils.exportExcel(new File(BASEPATH + "保研加分学生汇总表.xlsx"));
        fileService.downloadFile("保研加分学生汇总表.xlsx", request, response);
        fileService.deleteFile(BASEPATH + "保研加分学生汇总表.xlsx");
    }


}
